package com.seisys.controller.system.role;


import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seisys.controller.base.BaseController;
import com.seisys.entity.Page;
import com.seisys.entity.system.Menu;
import com.seisys.entity.system.Role;
import com.seisys.service.system.LogService;
import com.seisys.service.system.menu.MenuService;
import com.seisys.service.system.role.RoleService;
import com.seisys.util.AppUtil;
import com.seisys.util.Const;
import com.seisys.util.Jurisdiction;
import com.seisys.util.LogUtil;
import com.seisys.util.PageData;
import com.seisys.util.RightsHelper;
import com.seisys.util.Tools;

import net.sf.json.JSONArray;


/**
 * 类名称：RoleController 创建人：FH 创建时间：2014年6月30日
 * 
 * @version
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController extends BaseController
{
    private static final String MODULENAME = "角色管理";

    String menuUrl = "role.do"; // 菜单地址(权限用)

    @Resource(name = "menuService")
    private MenuService menuService;

    @Resource(name = "roleService")
    private RoleService roleService;

    @Resource(name = "logService")
    private LogService logService;

    /**
     * 权限(增删改查)
     */
    @RequestMapping(value = "/qx")
    public ModelAndView qx()
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        try
        {
            pd = this.getPageData();
            String msg = pd.getString("msg");
            if (Jurisdiction.buttonJurisdiction(menuUrl, "edit"))
            {
                roleService.updateQx(msg, pd);
            }
            mv.setViewName("save_result");
            mv.addObject("msg", "success");
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * K权限
     */
    @RequestMapping(value = "/kfqx")
    public ModelAndView kfqx()
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        try
        {
            pd = this.getPageData();
            String msg = pd.getString("msg");
            if (Jurisdiction.buttonJurisdiction(menuUrl, "edit"))
            {
                roleService.updateKFQx(msg, pd);
            }
            mv.setViewName("save_result");
            mv.addObject("msg", "success");
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * c权限
     */
    @RequestMapping(value = "/gysqxc")
    public ModelAndView gysqxc()
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        try
        {
            pd = this.getPageData();
            String msg = pd.getString("msg");
            if (Jurisdiction.buttonJurisdiction(menuUrl, "edit"))
            {
                roleService.gysqxc(msg, pd);
            }
            mv.setViewName("save_result");
            mv.addObject("msg", "success");
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * 列表
     */
    @RequestMapping
    public ModelAndView list(Page page)
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();

        // 列出所有角色
        List<Role> roleList = roleService.listAllRoles(pd);
        //
        // List<PageData> kefuqxlist = roleService.listAllkefu(pd); // 管理权限列表
        // List<PageData> gysqxlist = roleService.listAllGysQX(pd); // 用户权限列表
        // pd = roleService.findObjectById(pd); // 取得点击部门
        mv.addObject("pd", pd);
        // mv.addObject("kefuqxlist", kefuqxlist);
        // mv.addObject("gysqxlist", gysqxlist);
        mv.addObject("roleList", roleList);
        mv.setViewName("system/role/role_list");
        // mv.addObject(Const.SESSION_QX, this.getHC()); // 按钮权限
        return mv;
    }

    /**
     * 新增页面
     */
    @RequestMapping(value = "/toAdd")
    public ModelAndView toAdd(Page page)
    {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        try
        {
            pd = this.getPageData();
            mv.setViewName("system/role/role_add");
            mv.addObject("pd", pd);
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * 保存新增信息
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView add()
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        try
        {
            pd = this.getPageData();
            String parent_id = "0";
            // String parent_id = pd.getString("PARENT_ID"); // 父类角色id
            pd.put("ROLE_ID", parent_id);
            // if ("0".equals(parent_id)) {
            pd.put("RIGHTS", "");
            // } else {
            // String rights =
            // roleService.findObjectById(pd).getString("RIGHTS");
            // pd.put("RIGHTS", (null == rights) ? "" : rights);
            // }

            pd.put("QX_ID", "");

            String UUID = this.get32UUID();

            pd.put("GL_ID", UUID);
            pd.put("QX3", 0); // 预留权限
            pd.put("QX4", 0); // 预留权限
            if (Jurisdiction.buttonJurisdiction(menuUrl, "add"))
            {
                roleService.saveKeFu(pd);
            } // 保存到K权限表

            pd.put("U_ID", UUID);
            pd.put("C1", 0); // 每日发信数量
            pd.put("C2", 0);
            pd.put("C3", 0);
            pd.put("C4", 0);
            pd.put("Q1", 0); // 权限1
            pd.put("Q2", 0); // 权限2
            pd.put("Q3", 0);
            pd.put("Q4", 0);
            if (Jurisdiction.buttonJurisdiction(menuUrl, "add"))
            {
                roleService.saveGYSQX(pd);
            } // 保存到G权限表
            pd.put("QX_ID", UUID);

            pd.put("ROLE_ID", UUID);
            pd.put("ADD_QX", "0");
            pd.put("DEL_QX", "0");
            pd.put("EDIT_QX", "0");
            pd.put("CHA_QX", "0");
            pd.put("OFFICE_ID", pd.get("OFFICE_ID").toString());
            if (Jurisdiction.buttonJurisdiction(menuUrl, "add"))
            {
                roleService.add(pd);
                LogUtil.getInstance().operationLogger(LogUtil.ADD, MODULENAME,
                    pd.getString("ROLE_ID"), logService, request);
            }
            mv.addObject("msg", "success");
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
            mv.addObject("msg", "failed");
        }
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 请求编辑
     */
    @RequestMapping(value = "/toEdit")
    public ModelAndView toEdit(String ROLE_ID)
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        try
        {
            pd = this.getPageData();
            pd.put("ROLE_ID", ROLE_ID);
            pd = roleService.findObjectById(pd);
            mv.setViewName("system/role/role_edit");
            mv.addObject("pd", pd);
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit()
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        try
        {
            pd = this.getPageData();
            if (Jurisdiction.buttonJurisdiction(menuUrl, "edit"))
            {
                String roleId = pd.getString("ROLE_ID");
                pd = roleService.edit(pd);
                LogUtil.getInstance().operationLogger(LogUtil.UPDATE, MODULENAME, roleId,
                    logService, request);
            }
            mv.addObject("msg", "success");
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
            mv.addObject("msg", "failed");
        }
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 请求角色菜单授权页面
     */
    @RequestMapping(value = "/auth")
    public String auth(@RequestParam String ROLE_ID, Model model)
        throws Exception
    {

        try
        {
            List<Menu> menuList = menuService.listAllMenu();
            Role role = roleService.getRoleById(ROLE_ID);
            String roleRights = role.getRIGHTS();
            if (Tools.notEmpty(roleRights))
            {
                for (Menu menu : menuList)
                {
                    menu.setHasMenu(RightsHelper.testRights(roleRights, menu.getMENU_ID()));
                    if (menu.isHasMenu())
                    {
                        List<Menu> subMenuList = menu.getSubMenu();
                        for (Menu sub : subMenuList)
                        {
                            sub.setHasMenu(RightsHelper.testRights(roleRights, sub.getMENU_ID()));
                            if (sub.isHasMenu())
                            {
                                List<Menu> triMenuList = sub.getSubMenu();
                                for (Menu triMenu : triMenuList)
                                {
                                    triMenu.setHasMenu(
                                        RightsHelper.testRights(roleRights, triMenu.getMENU_ID()));
                                }
                            }
                        }
                    }
                }
            }
            JSONArray arr = JSONArray.fromObject(menuList);
            String json = arr.toString();
            json = json.replaceAll("MENU_ID", "id").replaceAll("MENU_NAME", "name").replaceAll(
                "subMenu", "nodes").replaceAll("hasMenu", "checked");
            model.addAttribute("zTreeNodes", json);
            model.addAttribute("roleId", ROLE_ID);
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
        }

        return "authorization";
    }

    /**
     * 请求角色按钮授权页面
     */
    @RequestMapping(value = "/button")
    public ModelAndView button(@RequestParam String ROLE_ID, @RequestParam String msg, Model model)
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        try
        {
            List<Menu> menuList = menuService.listAllMenu();
            Role role = roleService.getRoleById(ROLE_ID);

            String roleRights = "";
            if ("add_qx".equals(msg))
            {
                roleRights = role.getADD_QX();
            }
            else if ("del_qx".equals(msg))
            {
                roleRights = role.getDEL_QX();
            }
            else if ("edit_qx".equals(msg))
            {
                roleRights = role.getEDIT_QX();
            }
            else if ("cha_qx".equals(msg))
            {
                roleRights = role.getCHA_QX();
            }

            if (Tools.notEmpty(roleRights))
            {
                for (Menu menu : menuList)
                {
                    menu.setHasMenu(RightsHelper.testRights(roleRights, menu.getMENU_ID()));
                    if (menu.isHasMenu())
                    {
                        List<Menu> subMenuList = menu.getSubMenu();
                        for (Menu sub : subMenuList)
                        {
                            sub.setHasMenu(RightsHelper.testRights(roleRights, sub.getMENU_ID()));
                        }
                    }
                }
            }
            JSONArray arr = JSONArray.fromObject(menuList);
            String json = arr.toString();
            // System.out.println(json);
            json = json.replaceAll("MENU_ID", "id").replaceAll("MENU_NAME", "name").replaceAll(
                "subMenu", "nodes").replaceAll("hasMenu", "checked");
            mv.addObject("zTreeNodes", json);
            mv.addObject("roleId", ROLE_ID);
            mv.addObject("msg", msg);
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
        }
        mv.setViewName("system/role/role_button");
        return mv;
    }

    /**
     * 保存角色菜单权限
     */
    @RequestMapping(value = "/auth/save")
    @ResponseBody
    public String saveAuth(@RequestParam String ROLE_ID, @RequestParam String menuIds)throws Exception
    {
        PageData pd = new PageData();
        try
        {
            if (Jurisdiction.buttonJurisdiction(menuUrl, "edit"))
            {
                if (null != menuIds && !"".equals(menuIds.trim()))
                {
                    BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));
                    Role role = roleService.getRoleById(ROLE_ID);
                    role.setRIGHTS(rights.toString());
                    roleService.updateRoleRights(role);
                    pd.put("rights", rights.toString());

                }
                else
                {
                    Role role = new Role();
                    role.setRIGHTS("");
                    role.setROLE_ID(ROLE_ID);
                    roleService.updateRoleRights(role);
                    pd.put("rights", "");
                }

                pd.put("roleId", ROLE_ID);
                roleService.setAllRights(pd);
                LogUtil.getInstance().operationLogger("给菜单赋权限", MODULENAME, pd.getString("roleId"),
                    logService, request);
            }
            return "success";
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
            return "error";
        }
    }

    /**
     * 保存角色按钮权限
     */
    @RequestMapping(value = "/roleButton/save")
    @ResponseBody
    public String orleButton(@RequestParam String ROLE_ID, @RequestParam String menuIds,
                           @RequestParam String msg, PrintWriter out)
        throws Exception
    {
        PageData pd = new PageData();
        pd = this.getPageData();
        try
        {
            if (Jurisdiction.buttonJurisdiction(menuUrl, "edit"))
            {
                if (null != menuIds && !"".equals(menuIds.trim()))
                {
                    BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));
                    pd.put("value", rights.toString());
                }
                else
                {
                    pd.put("value", "");
                }
                pd.put("ROLE_ID", ROLE_ID);
                roleService.updateQx(msg, pd);
            }
            return "success";
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
            return "error";
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object deleteRole(@RequestParam String ROLE_ID)
        throws Exception
    {
        Map<String, String> map = new HashMap<String, String>();
        PageData pd = new PageData();
        String errInfo = "";
        pd.put("ROLE_ID", ROLE_ID);
        try
        {
            if (Jurisdiction.buttonJurisdiction(menuUrl, "del"))
            {
                List<PageData> userlist = roleService.listAllUByRid(pd);
                if (userlist.size() > 0)
                {
                    errInfo = "false";
                }
                else
                {
                    roleService.deleteRoleById(ROLE_ID);
                    roleService.deleteKeFuById(ROLE_ID);
                    roleService.deleteGById(ROLE_ID);
                    LogUtil.getInstance().operationLogger(LogUtil.DELETE, MODULENAME,
                        pd.getString("ROLE_ID"), logService, request);
                    errInfo = "success";
                }
            }
        }
        catch (Exception e)
        {
            logger.error(e.toString(), e);
        }
        map.put("result", errInfo);
        return AppUtil.returnObject(new PageData(), map);
    }

    /* ===============================权限================================== */
    public Map<String, String> getHC()
    {
        Subject currentUser = SecurityUtils.getSubject(); // shiro管理的session
        Session session = currentUser.getSession();
        return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
    }

    /* ===============================权限================================== */

    @RequestMapping(value = "/getOfficeList")
    @ResponseBody
    public Object getOfficeList()
        throws Exception
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PageData pd = new PageData();
        pd = this.getPageData();
        map.put("result", roleService.getOfficeList(pd));
        return map;
    }
}
